﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace StringSearch
{
    public partial class StringSearch : Form
    {
		private Matcher _matcher;
        public StringSearch()
        {
            InitComponents();
        }

        private void _miOpen_Click(object sender, EventArgs e)
        {
            if (this._diaOpen.ShowDialog() == DialogResult.OK)
            {
                this.Text = String.Format("Search v1.0 - {0}", this._diaOpen.FileName);
                this._rbx.Text = File.ReadAllText(this._diaOpen.FileName, Encoding.ASCII);
//                 HorspoolMatcher matcher = new HorspoolMatcher("abc");
//                 matcher.Search(this._text);
//                 if (matcher.Founds != null)
//                 {
//                     MessageBox.Show("Test");
//                 }
//                 TinyMatcher matcher = new TinyMatcher(new string[] { "ace", "as", "ease" });
//                 matcher.Search(this._text);
//                 if (matcher.Founds != null)
//                 {
//                     foreach (KeyValuePair<int, int> found in matcher.Founds)
//                     {
//                         MessageBox.Show(String.Format("{0} : {1}.", found.Key, found.Value));
//                     }
//                 }
//                 AhoCorasickMatcher matcher = new AhoCorasickMatcher(new string[] { "ace", "as", "ease" });
//                 matcher.Search(this._text);
            }
        }

        private void InitComponents()
        {
            InitializeComponent();

            this._cbxAlgorithms.SelectedIndex = 0;
        }

        private void _cbxAlgorithms_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (this._cbxAlgorithms.SelectedIndex)
            {
				case 0:
					{
						this._btnAdd.Enabled = false;
						this._btnRemove.Enabled = false;
						this._lvPatterns.Enabled = false;
					}
					break;
				case 1:
				case 2:
					{
						this._btnAdd.Enabled = true;
						this._btnRemove.Enabled = true;
						this._lvPatterns.Enabled = true;
					}
					break;
            }
        }

		private void _btnSearch_Click(object sender, EventArgs e)
		{
			try
			{
				switch (this._cbxAlgorithms.SelectedIndex)
				{
					case 0: this._matcher = new HorspoolMatcher(new string[] { this._txtPattern.Text }); break;
					case 1:
						{
							string[] patterns = new string[this._lvPatterns.Items.Count];
							for (int i = 0; i < this._lvPatterns.Items.Count; i++)
							{
								patterns[i] = this._lvPatterns.Items[i].SubItems[0].Text;
							}
							this._matcher = new AhoCorasickMatcher(patterns);
						} break;
					case 2:
						{
							string[] patterns = new string[this._lvPatterns.Items.Count];
							for (int i = 0; i < this._lvPatterns.Items.Count; i++)
							{
								patterns[i] = this._lvPatterns.Items[i].SubItems[0].Text;
							}
							this._matcher = new TinyMatcher(patterns);
						} break;
				}
				this._matcher.Search(this._rbx.Text);
				int j = 0;
				this._lvFounds.Items.Clear();
				foreach (KeyValuePair<int, List<int>> r in this._matcher.Founds)
				{
					foreach (int i in r.Value)
					{
						ListViewItem item = new ListViewItem(new string[] { j.ToString(), this._matcher.Patterns[r.Key].ToString(), i.ToString() });
						this._lvFounds.Items.Add(item);
					}

					j++;
				}
			}
			catch (Exception)
			{
				return;
			}
		}

		private void _btnAdd_Click(object sender, EventArgs e)
		{
			if (this._txtPattern.Text != "" && !this._lvPatterns.Items.ContainsKey(this._txtPattern.Text)) {				
				this._lvPatterns.Items.Add(this._txtPattern.Text, this._txtPattern.Text, null);
			}

		}

		private void _btnRemove_Click(object sender, EventArgs e)
		{
			while (this._lvPatterns.SelectedItems.Count > 0)
			{
				this._lvPatterns.Items.Remove(this._lvPatterns.SelectedItems[0]);
			}
		}	
    }
}
